/**
 *@description:TODO
 * @author: cai
 *@time:2017年10月13日上午11:55:30
 */
package com.pos.modules.hang;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;

import com.pos.BaseActivity;
import com.pos.R;
import com.pos.modules.cashier.CashierActivity;
import com.pos.modules.cashier.bean.GoodsBean;
import com.pos.modules.hang.adapter.HangListAdapter;
import com.pos.modules.hang.adapter.HangListAdapter.HangAction;
import com.pos.modules.hang.bean.HangGoodsBean;
import com.pos.set.db.HangGoodsProvider;
import com.pos.set.db.HangGoodsProvider.GoodsConstants;
import com.pos.widgets.ActionBarManager;
import com.pos.widgets.swipe.CustomSwipeListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:取单主页
 * @author cai
 * @time:2017年10月13日上午11:55:30
 */
public class HangActivity extends BaseActivity {
	final static private String[] GOODS_PROJECTION = new String[] { GoodsConstants.DATE, GoodsConstants.NAME,
			GoodsConstants.GOODS };

	private List<HangGoodsBean> hangList = new ArrayList<>();
	private HangListAdapter adapter;
	private ContentResolver mContentResolver;
	private Gson gson;
	private CustomSwipeListView swipeListView;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void initView() {
		setContentView(R.layout.hang_layout);
		swipeListView = bindId(R.id.swipeListView);
	}

	@Override
	public void initData() {
		ActionBarManager.init(this, "取单", true, null, null);
		mContentResolver = getContentResolver();
		gson = new Gson();
		Cursor goodsCursor = mContentResolver.query(HangGoodsProvider.CONTENT_URI, GOODS_PROJECTION, null, null, null);
		if (goodsCursor != null && goodsCursor.moveToFirst()) {

			do {
				System.out.println("goodsCursor.getString(2)" + goodsCursor.getString(2));
				List<GoodsBean> goods = gson.fromJson(goodsCursor.getString(2), new TypeToken<List<GoodsBean>>() {
				}.getType());
				hangList.add(new HangGoodsBean(goodsCursor.getLong(0), goodsCursor.getString(1), goods));
			} while (goodsCursor.moveToNext());
			goodsCursor.close();
		}
		if (hangList == null) {
			hangList = new ArrayList<>();
		}
		adapter = new HangListAdapter(mContext, hangList, new HangAction() {

			@Override
			public void onGet(int position) {
				List<GoodsBean> goodsList = hangList.get(position).getGoodsList();
				Intent intent = new Intent(mContext, CashierActivity.class);
				intent.putExtra("goodsList", (ArrayList<GoodsBean>) goodsList);
				startActivity(intent);
				long time = hangList.get(position).getDate();
				mContentResolver.delete(HangGoodsProvider.CONTENT_URI, GoodsConstants.DATE + "='" + time + "'", null);
				hangList.remove(position);
				finish();
			}

			@Override
			public void onDelete(int position) {
				long time = hangList.get(position).getDate();
				mContentResolver.delete(HangGoodsProvider.CONTENT_URI, GoodsConstants.DATE + "='" + time + "'", null);
				hangList.remove(position);
			}
		});
		swipeListView.setAdapter(adapter);

	}

	@Override
	public void initListener() {
		// TODO Auto-generated method stub

	}

}
